package com.sport;

import java.util.Date;
import java.util.UUID;

import com.sport.model.User;
import com.sport.model.UserController;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Summary extends Activity {

	private int averageBPM;
	private int totalDenivele;
	private double averageSpeed;
	private double totalDistance;
	private String totalTime;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.summary);
		averageBPM = this.getIntent().getIntExtra("bpm",0);
		totalDenivele = this.getIntent().getIntExtra("denivele", 0);
		averageSpeed = this.getIntent().getDoubleExtra("speed", 0);
		totalDistance = this.getIntent().getDoubleExtra("distance", 0);
		totalTime = this.getIntent().getStringExtra("time");
		TextView bpm = (TextView) findViewById(R.id.averageBPM);
		TextView speed = (TextView) findViewById(R.id.averageSpeed);
		TextView distance = (TextView) findViewById(R.id.totalDistance);
		TextView denivele = (TextView) findViewById(R.id.totalDenivele);
		TextView time = (TextView) findViewById(R.id.totalTime);
		Button no = (Button) findViewById(R.id.no);
		Button yes = (Button) findViewById(R.id.yes);
		bpm.setText(""+averageBPM);
		speed.setText(""+averageSpeed);
		distance.setText(""+totalDistance);
		denivele.setText(""+totalDenivele);
		time.setText(totalTime);
		no.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				finish();
			}
		});
		yes.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				addUser();			
			}
		});
	}

	final void addUser()
	{
		final Thread checkUpdate = new Thread() {
			public void run() {
				User u = new User();
				u.setDistance(totalDistance);
				u.setBpm(averageBPM);
				u.setDate(new Date());
				u.setDenivele(totalDenivele);
				u.setSpeed(averageSpeed);
				u.setTime(totalTime);
				final UserController uc = new UserController();
				try {
					uc.create(u);
				} catch (Exception e) {
					return;
				}
				final Intent intent = new Intent(Summary.this, Home.class);
				startActivity(intent);
			}
		};
		checkUpdate.start();
	}
}